package org.budo.wxpay.util;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.budo.support.lang.util.StringUtil;
import org.budo.support.mvcs.Mvcs;

import com.github.wxpay.sdk.WXPay;
import com.github.wxpay.sdk.WXPayUtil;
import com.github.wxpay.sdk.budo.BudoWXPayConfig;

/**
 * @author lmw
 */
public class WxpayUtil_0 {
    /**
     * Mozilla/5.0 (Linux; Android 8.1.0; PBCM30 Build/OPM1.171019.011; wv) 
     * AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 
     * Chrome/66.0.3359.126 
     * MQQBrowser/6.2 TBS/044803 
     * Mobile Safari/537.36 
     * MMWEBID/5942 
     * MicroMessenger/7.0.4.1420(0x2700043C) 
     * Process/tools NetType/WIFI Language/en
     */
    public static boolean isWxUserAgent(HttpServletRequest request) {
        String userAgent = Mvcs.getUserAgent(request);
        return StringUtil.contains(userAgent, "MicroMessenger");
    }

    @SuppressWarnings("rawtypes")
    public static boolean isWxPayOk(Map rst) {
        if (rst == null) {
            return false;
        }

        if ("SUCCESS".equals(rst.get("return_code")) && "SUCCESS".equals(rst.get("result_code"))) {
            return true;
        } else {
            return false;
        }
    }

    public static String generateSignature(Map<String, String> data, String key) {
        try {
            return WXPayUtil.generateSignature(data, key);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public static WXPay newWXPay(BudoWXPayConfig config) {
        try {
            return new WXPay(config);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public static String MD5(String data) {
        try {
            return WXPayUtil.MD5(data);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public static Map<String, String> unifiedOrder(WXPay wxpay, Map<String, String> data) {
        try {
            return wxpay.unifiedOrder(data);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public static Map<String, String> orderQuery(WXPay wxpay, Map<String, String> requestMap) {
        try {
            return wxpay.orderQuery(requestMap);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}